package com.sgb.kjwl.view.ui.fragment.self.pick;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentSelfPickList3Binding;
import com.sgb.kjwl.di.self.DaggerSelfFragComponent;
import com.sgb.kjwl.di.self.SelfFragModule;
import com.sgb.kjwl.model.entity.self.SelfPickListEntity3;
import com.sgb.kjwl.view.event.CollegeEvent;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.self.pick.SelfPickListActivity3;
import com.sgb.kjwl.view.ui.adapter.self.pick.SelfCollectAdapter;
import com.sgb.kjwl.viewmodel.self.collect.SelfPickListFragModel3;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

/**
 * Description:
 * Author zhengkewen
 */
public class SelfPickListFragment3 extends AppFragment {
    public static String KEY_TYPE = "type";
    public static String KEY_STATUS = "status";
    private FragmentSelfPickList3Binding binding;
    private String type;
    private String status;
    private SelfPickListActivity3 selfPickListActivity3;
    private SelfCollectAdapter adapter;

    @Inject
    SelfPickListFragModel3 viewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerSelfFragComponent.builder()
                .appComponent(appComponent)
                .selfFragModule(new SelfFragModule(this))
                .build()
                .inject(this);
    }

    public static SelfPickListFragment3 newInstance(String type, String status) {
        SelfPickListFragment3 fragment = new SelfPickListFragment3();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_TYPE, type);
        bundle.putString(KEY_STATUS, status);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_self_pick_list3, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        type = getArguments().getString(KEY_TYPE);
        status = getArguments().getString(KEY_STATUS);
        initView();
        init();
    }

    private void initView() {
        if (adapter == null) {
            adapter = new SelfCollectAdapter(getActivity(), null);
        }
        adapter.setType(type);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_F5F5F5)));
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getList(false);
            }

            @Override
            public void onLoadMore() {
                viewModel.getList(true);
            }
        });
        binding.recyclerView.setAdapter(adapter);

        adapter.setOnItemClick(new AdapterOnItemClick<SelfPickListEntity3>() {
            @Override
            public void onItemClick(SelfPickListEntity3 selfPickListEntity3, int position) {
                viewModel.getH5Url(selfPickListEntity3.getNo(),1);//type 0 首页 1 我的集采 2 我的报价
            }
        });
        adapter.setItemCallBack(new SelfCollectAdapter.ItemCallBack() {
            @Override
            public void stop(String id) {
                String msg = "您确认删除此次集采？";
                new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                        .setTitle("温馨提示")
                        .setMsg(msg)
                        .setDialogCallBack(new CommonDialog.DialogCallBack() {
                            @Override
                            public void onRightClick() {
                                if (type.equals("1")) {
                                    viewModel.delList(id);
                                } else if (type.equals("2")) {
                                    viewModel.delList2(id);
                                }
                            }

                            @Override
                            public void onLeftClick() {

                            }
                        }).show();
            }

            @Override
            public void exit(String id) {

            }

            @Override
            public void reedit(String id) {

            }
        });
    }

    public void showUrl(String url){
        WebViewX5HostActivity.start(getActivity(),url);
    }

    private void init() {
        selfPickListActivity3 = (SelfPickListActivity3) getActivity();
        viewModel.status.set(status);
        viewModel.type.set(type);
        viewModel.getList(false);
    }

    public void showList(List<SelfPickListEntity3> list, boolean isLoadMore) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() != 0) {
                adapter.addDatas(list);
            }
        } else {
            if (list.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            binding.recyclerView.refreshComplete();
            binding.recyclerView.setLoadingMoreEnabled(true);
            adapter.setDatas(list);
        }
        if (adapter.getDatas() != null && adapter.getDatas().size() != 0) {
            selfPickListActivity3.changeTab(type,status, adapter.getDatas().size());
        } else {
            selfPickListActivity3.changeTab(type,status, 0);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(CollegeEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_COLLECT_SELF_LIST) {
            viewModel.getList(false);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
